//
//  HRDynamicViewController.m
//  Hear
//
//  Created by mac on 2017/3/26.
//  Copyright © 2017年 mac. All rights reserved.
// 个人主页

#import "HRDynamicViewController.h"
#import "HRBaseTableView.h"
#import "HRDynamicTableViewModel.h"
#import "HRDynamicViewModel.h"
#import "HRDynamicHeadView.h"
#import "HRPostDynamicViewController.h"
#import "HRDynamicModel.h"
#import "HRFollowDyanmicInfoViewController.h"
#import "HRGetUserinfoViewModel.h"
#import "HRUpdataUserInfoModel.h"
#import "HRUpdataUserInfoModel.h"
#import "HRDynamicBottomView.h"
#import "MJRefresh.h"
#import "HRChangeBackImageViewController.h"
#import "PopMenu.h"
#import "HRRecordAudioViewController.h"
#import "HRShowRecordView.h"
#import "HRShowGuideView.h"
//#import "UITableView+VideoPlay.h"
//#import "UIView+WebVideoCache.h"
#import "HRVideoPlayerCell.h"
//#import <QPSDK/QPSDK.h>
#import "HRPostVideoDynamicViewController.h"
#import "HRBombBoxBottomOfView.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
#import "HRReportViewController.h"
#import "HRDynamicSectionView.h"
#import "HRFunChatViewModel.h"
#import "HRFunChatModel.h"
#import "Track.h"
#import "MTMusicFeature.h"
#import "HRPostSoundViewModel.h"
#import "HRTopicListLayout.h"
@interface HRDynamicViewController ()<HRDynamicTableViewModelDelegate,UIAlertViewDelegate,UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic, strong) HRBaseTableView *tableView;
@property (nonatomic, strong) HRDynamicTableViewModel *tableViewModel;
@property (nonatomic, strong) HRDynamicViewModel *viewModel;
@property (nonatomic, strong) HRDynamicHeadView *headView;
@property (nonatomic, assign) CGFloat headViewHeight;
@property (nonatomic, strong) HRGetUserinfoViewModel *getUserInfoModel;
@property (nonatomic, strong) HRUpdataUserInfoModel *infModel;
@property (nonatomic, strong) HRDynamicBottomView *bottomView;
@property (nonatomic, assign) NSInteger pIndex;
@property (nonatomic, strong) PopMenu *popMenu;
@property (nonatomic, assign) CGFloat tempOffset;
@property(nonatomic, strong, nonnull) UIView *tableViewRange;
@property(nonatomic, strong)UIView *navView;
@property(nonatomic, strong)NSString *followStatusStr;
@property(nonatomic, strong)UILabel *titleLabel;

@property(nonatomic, strong)HRDynamicSectionView *sectionView;
@property (nonatomic, strong)NSMutableArray *hearContentArr;
@property (nonatomic, assign)NSInteger selectIndex;
@property (nonatomic, strong)NSMutableArray *dynamicContentArr;
@property (nonatomic, strong)NSMutableArray *funchatContentArr;

@property(nonatomic, strong)HRFunChatViewModel *chatViewModel;

@property(nonatomic, strong)HRPostSoundViewModel *saveSoundModel;

@end

@implementation HRDynamicViewController
#pragma mark - Get and Set
- (HRPostSoundViewModel *)saveSoundModel{
    if (_saveSoundModel == nil) {
        _saveSoundModel = [[HRPostSoundViewModel alloc] init];
    }
    return _saveSoundModel;
}

- (HRFunChatViewModel *)chatViewModel{
    if (_chatViewModel == nil) {
        _chatViewModel = [[HRFunChatViewModel alloc] init];
    }
    return _chatViewModel;
}

- (NSMutableArray *)funchatContentArr{
    if (_funchatContentArr == nil) {
        _funchatContentArr = [[NSMutableArray alloc] init];
    }
    return _funchatContentArr;
}

- (NSMutableArray *)hearContentArr{
    if (_hearContentArr == nil) {
        _hearContentArr = [[NSMutableArray alloc] init];
    }
    return _hearContentArr;
}
- (NSMutableArray *)dynamicContentArr{
    if (_dynamicContentArr == nil) {
        _dynamicContentArr = [[NSMutableArray alloc] init];
    }
    return _dynamicContentArr;
}

- (HRDynamicSectionView *)sectionView{
    if (_sectionView == nil) {
        WeakSelf
        _sectionView = [[HRDynamicSectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 60)];
        _sectionView.selectIndexBlock = ^(NSInteger index) {
            weakSelf.selectIndex = index;
            [weakSelf.tableViewModel.contentArr removeAllObjects];
            if (index ==0){
                [weakSelf.tableViewModel.contentArr addObjectsFromArray:weakSelf.dynamicContentArr];
                [weakSelf.tableView reloadData];
            }else if (index == 1){
                [weakSelf.tableViewModel.contentArr addObjectsFromArray:weakSelf.hearContentArr];
                [weakSelf.tableView reloadData];
            }else if (index ==2){
                [weakSelf.tableViewModel.contentArr addObjectsFromArray:weakSelf.funchatContentArr];
                [weakSelf.tableView reloadData];
            }
        };
    }
    return _sectionView;
}

- (HRGetUserinfoViewModel *)getUserInfoModel{
    if (_getUserInfoModel == nil){
        _getUserInfoModel = [HRGetUserinfoViewModel new];
    }
    return _getUserInfoModel;
}

- (HRDynamicHeadView *)headView {
    if (_headView == nil) {
        WeakSelf
        _headView = [[HRDynamicHeadView alloc] initWithFrame:CGRectMake(0, 0, kFullWidth, self.headViewHeight) andCurrentUser:self.currentUserID];
        _headView.changeBackgroudImageBlock = ^(NSArray *imageArr){
            [weakSelf chageImage:imageArr];
        };
    }
    return _headView;
}

- (HRDynamicViewModel *)viewModel {
    if (_viewModel == nil)
    {
        _viewModel = [HRDynamicViewModel new];
    }
    return _viewModel;
}

- (HRDynamicTableViewModel *)tableViewModel {
    if (_tableViewModel == nil) {
        WeakSelf
        _tableViewModel = [HRDynamicTableViewModel new];
        _tableViewModel.delegate = self;
        _tableViewModel.contentOffsetBlock =^(CGFloat offsetY){
            [weakSelf contentOffset:offsetY];
        };
        _tableViewModel.changeNavigationAlphaBlock = ^(CGFloat alpha){
            if (alpha <=0.3){
                self.titleLabel.alpha = 0;
                weakSelf.navView.backgroundColor = [NavTinColor colorWithAlphaComponent:0];
            }else{
                self.titleLabel.alpha = alpha;
                weakSelf.navView.backgroundColor =[NavTinColor colorWithAlphaComponent:alpha];
            }
        };
        _tableViewModel.deleteHistoryAnswerBlock = ^(NSUInteger index) {
            HRDunamicLayout *layout = self.tableViewModel.contentArr[index];
            [weakSelf deleteUserTopic:layout.dynamicModel andCurrentIndex:index];
        };
    }
    return _tableViewModel;
}

- (void)deleteUserTopic:(HRDynamicModel *)model andCurrentIndex:(NSUInteger)index {
    NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
    paramDict[@"UserTopicLocalId"] = model.LocalId;
    paramDict[@"UserLocalId"] = USERID;
    WeakSelf
    [self.saveSoundModel deleteUserTopic:paramDict isSeccess:^(BOOL isSeccess) {
        if (isSeccess){
            [MBProgressHUD showMessage:@"已删除" finishBlock:nil];
            
            [weakSelf.tableViewModel.contentArr removeObjectAtIndex:index];
            [weakSelf.tableView deleteRow:index inSection:0 withRowAnimation:UITableViewRowAnimationRight];
            [weakSelf.tableView reloadData];
            if (weakSelf.tableViewModel.contentArr.count < 10){
                weakSelf.tableView.mj_footer.hidden = YES;
            }
            
        }
    }];
}


- (instancetype)init {
    self = [super init];
    self.tableView = [HRBaseTableView new];
    return self;
}

- (void)clickBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    WeakSelf
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;

    self.selectIndex = 0;
//    [self insertLineInScreenCenter];
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.headViewHeight = 340;
//    [self.view addSubview:self.headView];

    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];

    self.tableView.tableHeaderView = self.headView;
    
    [self.tableViewModel handleWithTable:self.tableView];
    
    HRDynamicBottomView *bottomView = [HRDynamicBottomView new];
    bottomView.infoModel = self.infModel;
    [self.view addSubview:bottomView];
    if([self.currentUserID isEqualToString:USERID]){
        bottomView.hidden = YES;
    }
    self.bottomView = bottomView;
    bottomView.chatButtonBlock = ^(){
        [weakSelf clickChat];
    };
    
    bottomView.followBlock = ^{
        [weakSelf followAction];
    };
    
    bottomView.attentionButtonBlock = ^(){
        [weakSelf clickAttention];
    };

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view);
        make.left.right.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.view);

    }];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(40+kBottomHeight);
        make.bottom.equalTo(weakSelf.view);
    }];
    
    [self getUserInfo];

    [self getDynamicData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMyDynaminclist) name:@"reloadMyDynaminclist" object:nil];
    
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.estimatedRowHeight = 0;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kFullWidth, kStatusBarHeight+kNavBarHeight)];
    [self.view addSubview:self.navView];
    self.navView.backgroundColor = [NavTinColor colorWithAlphaComponent:0];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = CustomUIFont(15);
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = 1;
    [self.navView  addSubview:(self.titleLabel=titleLabel)];
    titleLabel.frame = CGRectMake(50, 20, 100, self.navView.height-20);
    titleLabel.centerX = self.navView.centerX;
    self.titleLabel.alpha = 0;

    UIButton *leftButton = [UIButton buttonWithType:0];
    leftButton.frame = CGRectMake(10, kStatusBarHeight, 40, 40);
    [leftButton setImage:[UIImage imageNamed:@"Back-Arrow"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:leftButton];
    
    UIButton *rightButton = [UIButton buttonWithType:0];
    rightButton.frame = CGRectMake(self.navView.width-50, leftButton.top, 40, 40);;
    rightButton.titleLabel.font = CustomUIFont(13);
    [rightButton addTarget:self action:@selector(postDynamicClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:rightButton];
    
    if ([self.currentUserID isEqualToString:USERID]){
        [rightButton setImage:[UIImage imageNamed:@"发帖按钮"] forState:0];
        
    }else{
        [rightButton setImage:[UIImage imageNamed:@"chat_更多"] forState:0];
    }
    if ([self.currentUserID isEqualToString:@"admin"] &&![self.currentUserID isEqualToString:USERID]){
        rightButton.hidden = YES;
    }
    
    
    if(![self.currentUserID isEqualToString:USERID]){
        BOOL firstLaunch = [[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstMain"];
        if (!firstLaunch) {
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            UIImageView *shadowimg = [[UIImageView alloc] initWithImage:GetImgWithStr(@"个人主页")];
            shadowimg.contentMode = UIViewContentModeScaleAspectFill;
            shadowimg.frame = window.bounds;
            shadowimg.userInteractionEnabled = YES;
            [window addSubview:shadowimg];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstEnterTap)];
            [shadowimg addGestureRecognizer:tap];
        }
    }
}


- (void)firstEnterTap {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isFirstMain"];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    for (id view in [window subviews]) {
        if([view isKindOfClass:[UIImageView class]]){
            
            UIImageView *bg = (UIImageView *)view;
            [UIView animateWithDuration:0.3f animations:^{
                bg.alpha = 0;
            } completion:^(BOOL finished) {
                [bg removeFromSuperview];
            }];
            
        }
    }
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return NO;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"暂无动态"];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return 150;
}

- (void)getUserFollowInfo{
    if (!self.currentUserID.length)return;
    WeakSelf
//    [self.getUserInfoModel getUserFollowInfo:@{@"CurrentUserLocalId":USERID,@"FollowedUserLocalId":self.currentUserID} andTimeCount:^(NSInteger timeCount) {
//        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
//        if (timeCount ==100000){
//            self.followStatusStr = @"添加好友";
//        }else{
//            self.followStatusStr = @"解除好友关系";
//        }
//    }];
    
    [self.getUserInfoModel judgeUserFollowInfo:@{@"CurrentUserLocalId":USERID,@"FollowedUserLocalId":self.currentUserID} andTimeCount:^(NSInteger timeCount) {
        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
        if (timeCount == 100000){
            self.followStatusStr = @"关注";
            [self.bottomView.followBtn setTitle:@"关注" forState:UIControlStateNormal];
        }else{
            self.followStatusStr = @"取消关注";
            [self.bottomView.followBtn setTitle:@"取消关注" forState:UIControlStateNormal];
        }
    }];
}


- (void)getUserInfo{
    
    WeakSelf
    [self.getUserInfoModel getUserInfoRequestModel:@{@"CurrentUserLocalId":USERID,@"VisitedUserLocalId":self.currentUserID} isSeccess:^(HRUpdataUserInfoModel *infModel) {
        weakSelf.infModel = infModel;
        weakSelf.bottomView.infoModel = infModel;
        weakSelf.headView.userInfoModel = infModel;
        weakSelf.titleLabel.text = infModel.NickName;
        [MBProgressHUD hideHUDForView:WINDOW animated:YES];

    }];
}

- (void)getDynamicData{
    self.pIndex = 0;

    [self getDynamicData:YES];
    [self getMySound:YES];
    [self getFunChatData:YES];
    
    WeakSelf
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pIndex = 0;
        if (weakSelf.selectIndex == 0){
            [weakSelf getDynamicData:YES];
        }else if (weakSelf.selectIndex == 1){
            [weakSelf getMySound:YES];
        }else if (weakSelf.selectIndex == 2){
            [weakSelf getFunChatData:YES];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    // 上拉加载
    self.tableView.mj_footer=[MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        weakSelf.pIndex ++;
        if (weakSelf.selectIndex == 0){
            [weakSelf getDynamicData:NO];
        }else if (weakSelf.selectIndex == 1){
            [weakSelf getMySound:NO];
        }else if (weakSelf.selectIndex == 2){
            [weakSelf getFunChatData:NO];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    }];

    
}


- (void)getDynamicData:(BOOL)isHead{
    HRDynamicRequestModel *requstModel = [HRDynamicRequestModel new];
    requstModel.UserLocalId = self.currentUserID;
    requstModel.CurrentUserLocalId = USERID;
    requstModel.PageSize = 10;
    requstModel.PageIndex = self.pIndex;
    WeakSelf
    [self.viewModel getDynamicRequestModel:requstModel contentArr:^(NSMutableArray *contentArr) {

        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
        if (isHead){
            [weakSelf.tableViewModel.contentArr removeAllObjects];
        }
        [weakSelf.tableViewModel.contentArr addObjectsFromArray:contentArr];
        
        [weakSelf.dynamicContentArr addObjectsFromArray:contentArr];
        [weakSelf.tableView reloadData];
        
        if (contentArr.count < requstModel.PageSize){
            weakSelf.tableView.mj_footer.hidden = YES;
        }else{
            
            weakSelf.tableView.mj_footer.hidden = NO;
        }
    }];
}
                            

#pragma HRDynamicTableViewModelDelegate

-(UIView *)tableViewDynamicTableViewModelDelegate:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.sectionView;
}

- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath andDynamicModel:(HRDynamicModel *)dynamicModel{
    
    if(!dynamicModel.Permission && ![dynamicModel.UserLocalId isEqualToString:USERID]){
        return;
    }
    if ([dynamicModel.MomentType intValue] == 6){
        return;
    }
    WeakSelf
    HRFollowDyanmicInfoViewController *infoVC = [HRFollowDyanmicInfoViewController new];
    HRFollowDynamicLayout *detailLayout = [HRFollowDynamicLayout new];
    infoVC.isMyDynamic = YES;
    detailLayout.style = HRFollowLayoutStyleDetail;
    detailLayout.dynamicModel = dynamicModel;
    infoVC.layout = detailLayout;
    infoVC.reloadListBlock = ^(NSMutableArray *arr){
        [weakSelf.tableView reloadRow:indexPath.row inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    };
    infoVC.removeCircleBlock = ^{
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"cacheTime"];
        [weakSelf.tableViewModel.contentArr removeObjectAtIndex:indexPath.row];
        [weakSelf.tableView reloadData];
    };
    [self.navigationController pushViewController:infoVC animated:YES];
    
}


- (void)postDynamicClick{
    if ([self.currentUserID isEqualToString:USERID]){
        // 个人主页
        [self showMenu];
    } else {
        // 他人主页
        WeakSelf
//        [HRBombBoxBottomOfView showBombBoxBottomOfView:@[self.followStatusStr,@"举报",@"拉黑",@"取消"] selectindexBlock:^(NSInteger index) {
//            if(index == 1){
//                if ([weakSelf.followStatusStr isEqualToString:@"添加好友"]){
//                    [weakSelf saveUserFollow];
//                }else{
//                    [weakSelf deleteUserFollow];
//                }
//            }else if (index == 2){
//                HRReportViewController *VC = [HRReportViewController new];
//                VC.ReportUserLocalId = weakSelf.currentUserID;
//                [weakSelf.navigationController pushViewController:VC animated:YES];
//            }else if (index == 3){
//                [weakSelf saveBlackList];
//            }
//        } addSubView:WINDOW];
        
        [HRBombBoxBottomOfView showBombBoxBottomOfView:@[@"举报",@"拉黑",@"取消"] selectindexBlock:^(NSInteger index) {
             if (index == 1){
                HRReportViewController *VC = [HRReportViewController new];
                VC.ReportUserLocalId = weakSelf.currentUserID;
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
            if (index == 2) {
                [weakSelf saveBlackList];
            }
        } addSubView:WINDOW];
    }
    
}

- (void)saveBlackList{
    NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
    paramDict[@"LocalId"] = USERID;
    paramDict[@"InformantUser"] = USERID;
    paramDict[@"BlackUser"] = self.currentUserID;
    paramDict[@"Reason"] = @"4";
    paramDict[@"Note"] = @"内容";
    paramDict[@"CreateTime"] = [self getCurrentTime];
    
    WeakSelf
    [self.viewModel postSaveBlackList:paramDict isSeccess:^(BOOL isSeccess) {
        if (isSeccess){
            [MBProgressHUD showMessage:@"拉黑成功" finishBlock:nil];
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
    
    EMError *error = [[EMClient sharedClient].contactManager addUserToBlackList:self.currentUserID relationshipBoth:YES];
    if (!error) {
        DDLog(@"发送成功");
        [[EMClient sharedClient].chatManager deleteConversation:self.currentUserID isDeleteMessages:YES completion:^(NSString *aConversationId, EMError *aError){
            //code
        }];
    }
}



- (NSString *)getCurrentTime {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    
    return [formatter stringFromDate:[NSDate date]];
}

- (void)clickMenu {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 关注
- (void)followAction {
    if ([self.followStatusStr isEqualToString:@"关注"]) {
        [self saveUserFollow];
    } else {
        [self deleteUserFollow];
    }
}

#pragma mark 私聊
- (void)clickChat {
    if (!self.infModel) return;
    EaseMessageViewController *messgaeVC = [[EaseMessageViewController alloc] initWithConversationChatter:self.currentUserID conversationType:EMConversationTypeChat];
    messgaeVC.otherNickNameStr = self.infModel.UserRemark.length>0?self.infModel.UserRemark:self.infModel.NickName;
    messgaeVC.otherIconStr = self.infModel.UserPictureBackgroundColor;
    messgaeVC.otherIconName = self.infModel.UserPictureName;
    messgaeVC.title = self.infModel.UserRemark.length>0?self.infModel.UserRemark:self.infModel.NickName;
    [self.navigationController pushViewController:messgaeVC animated:YES];
}

- (void)clickAttention {

}

- (void)deleteUserFollow {
    WeakSelf
//    [self.viewModel deleteUserFollow:@{@"CurrentUserLocalId":USERID,@"FollowedUserLocalId":self.currentUserID} isSeccess:^(BOOL isSeccess) {
//        if (isSeccess){
//            weakSelf.followStatusStr = @"添加好友";
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [MBProgressHUD showMessage:@"解除好友关系成功" finishBlock:nil];
//            });
//        }
//    }];
    
    [self.viewModel cancelUserFollow:@{@"CurrentUserLocalId":USERID,@"FollowedUserLocalId":self.currentUserID} isSeccess:^(BOOL isSeccess) {
        if (isSeccess){
            weakSelf.followStatusStr = @"关注";
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.bottomView.followBtn setTitle:@"关注" forState:UIControlStateNormal];
            });
        }
    }];
}

- (void)saveUserFollow {
    HRSaveUserFollowRequestModel *requestModel = [HRSaveUserFollowRequestModel new];
    requestModel.CurrentUserLocalId = USERID;
    requestModel.FollowedUserLocalId = self.currentUserID;
    requestModel.CreateTime = [self getCurrentTime];
//    [self.viewModel saveUserFollow:requestModel isSeccess:^(BOOL isSeccess) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [MBProgressHUD showMessage:@"您的好友邀请已发送" finishBlock:nil];
//        });
//    }];
    [self.viewModel followUserFollow:requestModel isSeccess:^(BOOL isSeccess) {
        if (isSeccess) {
            self.followStatusStr = @"取消关注";
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.bottomView.followBtn setTitle:@"取消关注" forState:UIControlStateNormal];
            });
        }
    }];
}

- (void)chageImage:(NSArray *)imageArr{
    HRChangeBackImageViewController *VC = [HRChangeBackImageViewController new];
    VC.imageArr = imageArr;
    [self.navigationController pushViewController:VC animated:YES];
    WeakSelf
    VC.changeImageBlock = ^(){
        [weakSelf.headView reloadBackImageView];
    };
}

- (void)showMenu{
    NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:3];
    
    MenuItem *menuItem = [MenuItem itemWithTitle:@"图文动态" iconName:@"广场悬浮_心情_触摸_I"];
    [items addObject:menuItem];
    
    menuItem = [MenuItem itemWithTitle:@"语音动态" iconName:@"广场悬浮_语音_触摸_I"];
    [items addObject:menuItem];
    
//    menuItem = [MenuItem itemWithTitle:@"视频动态" iconName:@"广场悬浮_语音_触摸_I"];
//    [items addObject:menuItem];

    
    if (!_popMenu) {
        _popMenu = [[PopMenu alloc] initWithFrame:self.view.bounds items:items];
        _popMenu.menuAnimationType = kPopMenuAnimationTypeNetEase;
        _popMenu.perRowItemCount = 2;
    }
    if (_popMenu.isShowed) {
        return;
    }
    WeakSelf
    _popMenu.didSelectedItemCompletion = ^(MenuItem *selectedItem) {
        if ([selectedItem.title isEqualToString:@"图文动态"]){
            HRPostDynamicViewController *postDynamicVC = [HRPostDynamicViewController new];
            postDynamicVC.clickReloadBlock = ^(){
                [weakSelf getDynamicData:YES];
            };
            [weakSelf.navigationController pushViewController:postDynamicVC animated:YES];
        }else if ([selectedItem.title isEqualToString:@"语音动态"]){
            HRRecordAudioViewController *audioVC = [HRRecordAudioViewController new];
            audioVC.title = @"瞬间";
            audioVC.isFromMine = YES;
            [weakSelf.navigationController pushViewController:audioVC animated:YES];
        }else if ([selectedItem.title isEqualToString:@"视频动态"]){
//            QupaiSDK *sdk = [QupaiSDK shared];
//            [sdk setDelegte:(id<QupaiSDKDelegate>)self];
//            sdk.enableMoreMusic = NO;
//            sdk.thumbnailCompressionQuality = 1;
//            sdk.enableVideoEffect=NO;
//            /* 基本设置 */
//            UIViewController *recordController = [sdk createRecordViewControllerWithMinDuration:1
//                                                                                    maxDuration:300
//                                                                                        bitRate:2000*1000];
//            recordController.hidesBottomBarWhenPushed = YES;
//            UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:recordController];
//            navigation.navigationBarHidden = YES;
//            [self presentViewController:navigation animated:YES completion:nil];
            
        }
    };
    
    [_popMenu showMenuAtView:self.view];
}

- (void)contentOffset:(CGFloat)offsetY{
    DDLog(@"%f",offsetY);
    WeakSelf
    if (offsetY < weakSelf.tempOffset){
        [UIView animateWithDuration:0.4 animations:^{
            weakSelf.bottomView.alpha = 1;
        }];
    }else{
        [UIView animateWithDuration:0.4 animations:^{
            weakSelf.bottomView.alpha = 0;
        }];
    }
    weakSelf.tempOffset = offsetY;
}


#pragma 发布语音 通知当前视图刷新
- (void)reloadMyDynaminclist{
    [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"cacheTime"];
    [self getDynamicData:YES];
}


- (void)newUserGuide{
    if ([self.currentUserID isEqualToString:USERID])return;
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"照片墙提示页面"])return;
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"照片墙提示页面"];
    
    CGRect frame = [UIScreen mainScreen].bounds;
    UIView * bgView = [[UIView alloc]initWithFrame:frame];
    bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sureTapClick:)];
    [bgView addGestureRecognizer:tap];
    [[UIApplication sharedApplication].keyWindow addSubview:bgView];
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:frame];
    
    [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, kFullHeight-40, kFullWidth, 40) cornerRadius:5] bezierPathByReversingPath]];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath; //
    shapeLayer.strokeColor = [UIColor blueColor].CGColor;
    [bgView.layer setMask:shapeLayer];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake((kFullWidth-179)/2,kFullHeight-50-90,179, 90)];
    imageView.image = [UIImage imageNamed:@"引导_关注"];
    [bgView addSubview:imageView];
}

- (void)sureTapClick:(UITapGestureRecognizer *)tap {
    UIView * view = tap.view;
    [view removeFromSuperview];
    [view removeAllSubviews];
    [view removeGestureRecognizer:tap];
}

-(void)insertLineInScreenCenter{
    [[UIApplication sharedApplication].keyWindow insertSubview:self.tableViewRange aboveSubview:self.tableView];
}



-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"cacheTime"];
    
    // 判断当前用户和点击用户关系
    if (![self.currentUserID isEqualToString:USERID]){
        [self getUserFollowInfo];
    }
    
    if ([self.currentUserID isEqualToString:USERID]){
        if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"UserCoverPicture"] length]){
            UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"" message:@"您的主页背景墙盼着您来丰富哦，大\n方展现自己美好的一面吧！" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"继续浏览" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [cancelAction setValue:SecondTextColor forKey:@"titleTextColor"];
            
            [alertView addAction:cancelAction];
            WeakSelf
            UIAlertAction *enterAction = [UIAlertAction actionWithTitle:@"上传照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                HRChangeBackImageViewController *VC = [HRChangeBackImageViewController new];
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }];
            [enterAction setValue:BlueColor forKey:@"titleTextColor"];
            [alertView addAction:enterAction];
            [self presentViewController:alertView animated:YES completion:nil];
        }
    }
    
}

- (void)getMySound:(BOOL)isHead{
    WeakSelf
    NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
    paramDict[@"CurrentUserLocalId"] = USERID;
    paramDict[@"VisitedUserLocalId"] = self.currentUserID;
    paramDict[@"PageSize"] = @"10";
    paramDict[@"PageIndex"] = [NSString stringWithFormat:@"%d",self.pIndex];
    [self.viewModel getMySoundRequestDict:paramDict contentArr:^(NSMutableArray *contentArr) {
        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
        if (isHead){
            [weakSelf.hearContentArr removeAllObjects];
        }
        [weakSelf.hearContentArr addObjectsFromArray:contentArr];
        [weakSelf.tableViewModel.contentArr removeAllObjects];
        [weakSelf.tableViewModel.contentArr addObjectsFromArray:weakSelf.hearContentArr];
        [weakSelf.tableView reloadData];
        if (weakSelf.hearContentArr.count <10){
            weakSelf.tableView.mj_footer.hidden = YES;
        }
    }];
}

- (void)getFunChatData:(BOOL)isHead{
    NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
    paramDict[@"PageSize"] = @"10";
    paramDict[@"PageIndex"] = [NSString stringWithFormat:@"%d",self.pIndex];
    paramDict[@"CurrentUserLocalId"] = self.currentUserID;
    WeakSelf
    [self.chatViewModel getMyUserTopicList:paramDict contentArr:^(NSMutableArray *contentArr) {
        if (contentArr){
            if (isHead){
                [weakSelf.funchatContentArr removeAllObjects];
            }
            NSMutableArray *layArray = [NSMutableArray array];
            for (HRTopicListLayout *layount in contentArr) {
                HRFunChatModel *model = layount.chatModel;
                HRDynamicModel *dynamicModel = [HRDynamicModel new];
                dynamicModel.MomentType = @"3";
                if (model.SoundUrl.length){
                    dynamicModel.MomentType = @"1";
                    dynamicModel.MomentDetai = model.SoundUrl;
                    Track *track = [[Track alloc] init];
                    track.audioFileURL = [NSURL URLWithString:model.SoundUrl];
                    dynamicModel.musicFeature = [[MTMusicFeature alloc] initMusicFeatureWithAudioFile:track];
                }
                dynamicModel.SoundUrl = model.SoundUrl;
                dynamicModel.SoundLength = model.SoundLength;
                dynamicModel.MomentNotes = model.TopicTitle;
                dynamicModel.CreateTime = model.CreateTime;
                dynamicModel.LocalId = model.LocalId;
                dynamicModel.NickName = model.NickName;
                dynamicModel.UserPictureName = model.UserPictureName;
                dynamicModel.Permission = @"1";
                
                if ([weakSelf.currentUserID isEqualToString:USERID]){
                    dynamicModel.isFromHistoryAnswer = YES;
                }
                HRDunamicLayout *layount = [[HRDunamicLayout alloc] initWithStatus:dynamicModel style:0];
                
                [layArray addObject:layount];
                
            }
            [weakSelf.funchatContentArr addObjectsFromArray:layArray];
            if (weakSelf.funchatContentArr.count <10){
                weakSelf.tableView.mj_footer.hidden = YES;
            }
        }
    }];
}

-(void)viewWillDisappear:(BOOL)animated{
    [[NSUserDefaults standardUserDefaults] setInteger:300 forKey:@"cacheTime"];

    self.navigationController.navigationBarHidden = NO;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlayVoiceNotification" object:nil];
    
}

-(void)viewDidAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
